﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace ControlLibrary
{
    /// <summary>
    /// PictureControl
    /// </summary>
    public partial class PictureControl : Control
    {
        private int m_nImageIndex = 0;

        /// <summary>
        /// Image 的索引
        /// </summary>
        public int ImageIndex
        {
            get { return m_nImageIndex; }
            set
            {
                m_nImageIndex = value;
                this.Invalidate();
            }
        }

        private Image backgroundImage;

        /// <summary>
        /// 背景图片
        /// </summary>
        public override Image BackgroundImage
        {
            get { return backgroundImage; }
            set { backgroundImage = value; }
        }

        /// <summary>
        /// 构造
        /// </summary>
        public PictureControl()
        {
        }

        /// <summary>
        /// 画图
        /// </summary>
        /// <param name="pe"></param>
        protected override void OnPaint(PaintEventArgs pe)
        {
            SolidBrush brush = new SolidBrush(SystemColors.ActiveCaption);
            pe.Graphics.FillRectangle(brush, this.ClientRectangle);
            brush.Dispose();

            if (this.BackgroundImage != null)
            {
                ImageAttributes attr = new ImageAttributes();
                attr.SetColorKey(Color.Magenta, Color.Magenta);
                pe.Graphics.DrawImage(
                    this.backgroundImage, 
                    this.ClientRectangle, 
                    m_nImageIndex * 16, 0, 16, 16, GraphicsUnit.Pixel, attr);
            }
            // 调用基类 OnPaint
            base.OnPaint(pe);
        }
    }
}
